package com.boot.security.server.controller.mffl.MfflInformation;

import com.boot.security.server.dao.mffl.*;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.*;
import com.boot.security.server.model.mffl.Vo.InformationAndReplyVo;
import com.boot.security.server.model.mffl.Vo.MfflInformationReplyVo;
import com.boot.security.server.model.mffl.Vo.MfflInformationVo;
import com.boot.security.server.model.mffl.Vo.ReplyAndCommentVo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.impl.mffl.MfflSystemMessages.IMfflSystemMessages;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.JPush.JPushUtil;
import com.boot.security.server.utils.mffl.MfflUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户端资讯")
@RestController
@RequestMapping("/client/mfflInformations")
public class Client_MfflInformationController {

    @Autowired
    private MfflInformationDao mfflInformationDao;

    @Autowired
    private MfflInfomationContentDao mfflInfomationContentDao;

    @Autowired
    private MfflInformationReplyDao mfflInformationReplyDao;

    @Autowired
    private MfflUserDao mfflUserDao;

    @Autowired
    private MfflInformationCommendandtreadDao mfflInformationCommendandtreadDao;

    @Autowired
    private MfflInformationCollectDao mfflInformationCollectDao;

    @Autowired
    private MfflTypeDao mfflTypeDao;

    @Autowired
    private IMfflSystemMessages MfflSystemMessages;


    @PostMapping(value = "/saveTopic")
    @ApiOperation(value = "发布话题")
    @Transactional
    public ReturnBean saveTopic(@ApiParam(value = "标题", name = "title", required = true) @RequestParam String title,
                                @ApiParam(value = "内容", name = "content", required = true) @RequestParam String content,
                                @ApiParam(value = "图片地址多个按分号分隔", name = "imageUrl", required = true) @RequestParam String imageUrl) {

        if (StringUtils.isEmpty(title)) {
            return new ReturnBean("201", "请输入标题");
        }
        if (StringUtils.isEmpty(content)) {
            return new ReturnBean("201", "请输入内容");
        }
        if (!StringUtils.isEmpty(imageUrl)) {
            if (!imageUrl.contains("/")) {
                imageUrl = "";
            } else {
                imageUrl = imageUrl.replace(",", ";");
            }
        }

        SysUser sysUser = UserUtil.getLoginUser();
        Date date = new Date();
        MfflInformation mfflInformation = new MfflInformation();
        mfflInformation.setIdentityStatus(1);
        mfflInformation.setUserId(sysUser.getId().intValue());
        mfflInformation.setType(19);
        mfflInformation.setTitle(title);
        mfflInformation.setImageUrl(imageUrl);
        mfflInformation.setCreateTime(date);
        mfflInformation.setUpdateTime(date);
        mfflInformation.setDeleteTime(date);
        String partialContent = MfflUtil.delHTMLTag(content);
        if (partialContent.length() > 250) {
            partialContent = partialContent.substring(0, 249);
        }
        mfflInformation.setPartialContent(partialContent);
        int status = this.mfflInformationDao.save(mfflInformation);
        ReturnBean returnBean = new ReturnBean();
        if (status == 1) {
            MfflInfomationContent mfflInfomationContent = new MfflInfomationContent();
            mfflInfomationContent.setInfoId(mfflInformation.getId().intValue());
            mfflInfomationContent.setContent(content);
            mfflInfomationContentDao.save(mfflInfomationContent);
            return returnBean;
        } else {
            returnBean.setReturnBean("105");
            return returnBean;
        }
    }


    @GetMapping(value = "/getInformationType")
    @ApiOperation(value = "资讯-资讯分类")
    public ReturnBean<MfflType> getInformationType() {

        List<MfflType> mfflTypeList = this.mfflTypeDao.getInformationType();

        return new ReturnBean("200", mfflTypeList, "成功");
    }


    @GetMapping(value = "/informationList")
    @ApiOperation(value = "资讯-资讯列表")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int"),
            @ApiImplicitParam(required = false, name = "title", value = "标题", paramType = "query"),
            @ApiImplicitParam(required = false, name = "type", value = "资讯类型", paramType = "query", dataType = "int")})

    public ReturnBean<MfflInformationVo> topicList(PageTableRequest request) {
        if (request.getOffset() == null) {
            return new ReturnBean("201", "offset错误");
        }
        if (request.getLimit() == null) {
            return new ReturnBean("201", "limit错误");
        }
        PageTableResponse tableResponse = new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflInformationDao.topicCount(request.getParams());
            }
        }, new ListHandler() {
            @Override
            public List<MfflInformationVo> list(PageTableRequest request) {
                return mfflInformationDao.topicList(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
        List<MfflInformationVo> mfflInformationVos = (List<MfflInformationVo>) tableResponse.getData();
        for (MfflInformationVo m : mfflInformationVos) {
            m.setImageUrlAll(imageUrlDispose(m.getImageUrl()));
        }
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(tableResponse);
        return returnBean;
    }

    @GetMapping(value = "/getInformationTitle")
    @ApiOperation(value = "资讯详情-用于添加回答查询标题(只返回标题和类型)")
    public ReturnBean<MfflInformation> getInformationTitle(@ApiParam(value = "资讯id", name = "informationId", required = true) @RequestParam long informationId) {
        MfflInformation mfflInformation = this.mfflInformationDao.getById(informationId);
        if (mfflInformation == null) {
            return new ReturnBean("201", "参数错误");
        }

        return new ReturnBean("200", mfflInformation, "成功");
    }


    @PostMapping(value = "/addReply")
    @ApiOperation(value = "资讯-添加回答")
    public ReturnBean addReply(@ApiParam(value = "资讯id", name = "informationId", required = true) @RequestParam long informationId,
                               @ApiParam(value = "回答内容", name = "content", required = true) @RequestParam String content) {

        MfflInformation mfflInformation = this.mfflInformationDao.getById(informationId);
        if (mfflInformation == null) {
            return new ReturnBean("201", "没有该资讯");
        }
        MfflUser mfflUser = this.mfflUserDao.getUserInfoAll(mfflInformation.getUserId());
        if (mfflUser == null) {
            return new ReturnBean("201", "发布资讯用户不存在");
        }

        if (StringUtils.isEmpty(content)) {
            return new ReturnBean("201", "请输入回答");
        }
        SysUser sysUser = UserUtil.getLoginUser();
        MfflInformationReply mfflInformationReply = new MfflInformationReply();
        mfflInformationReply.setInformationId(mfflInformation.getId().intValue());
        mfflInformationReply.setTitle(mfflInformation.getTitle());
        mfflInformationReply.setType(mfflInformation.getType());
        mfflInformationReply.setUserId(sysUser.getId().intValue());
        mfflInformationReply.setContent(content);
        mfflInformationReply.setTopid(0);
        int status = mfflInformationReplyDao.save(mfflInformationReply);
        if (status == 1) {
            MfflSystemMessages.sendMessage("您收到一条系统消息，内容为:您发布的资讯[" + mfflInformation.getTitle() + "]有人评论，请查看！", "7", mfflInformation.getId().intValue(), mfflInformation.getUserId());
            return new ReturnBean();
        } else {
            return new ReturnBean("105", "操作失败请稍后再试");
        }
    }


    @PostMapping(value = "/addReplyComment")
    @ApiOperation(value = "资讯-给回答的用户添加评论")
    @Transactional
    public ReturnBean addReplyComment(@ApiParam(value = "回答id", name = "replyId", required = true) @RequestParam long replyId,
                                      @ApiParam(value = "回答内容", name = "content", required = true) @RequestParam String content) {

        MfflInformationReply mfflInformationReply = this.mfflInformationReplyDao.getById(replyId);
        if (mfflInformationReply == null) {
            return new ReturnBean("201", "没有该回答");
        }
        MfflInformation mfflInformation = this.mfflInformationDao.getById(mfflInformationReply.getInformationId().longValue());
        if (mfflInformation == null) {
            return new ReturnBean("201", "没有该资讯");
        }

        MfflUser mfflUser = this.mfflUserDao.getUserInfoAll(mfflInformation.getUserId());
        if (mfflUser == null) {
            return new ReturnBean("201", "发布资讯用户不存在");
        }

        MfflUser mfflUserTwo = this.mfflUserDao.getUserInfoAll(mfflInformationReply.getUserId());
        if (mfflUserTwo == null) {
            return new ReturnBean("201", "发布资讯用户不存在");
        }


        if (StringUtils.isEmpty(content)) {
            return new ReturnBean("201", "请输入回答");
        }
        SysUser sysUser = UserUtil.getLoginUser();
        MfflInformationReply mfflInformationReplyComment = new MfflInformationReply();
        mfflInformationReplyComment.setInformationId(mfflInformationReply.getInformationId());
        mfflInformationReplyComment.setTitle(mfflInformationReply.getTitle());
        mfflInformationReplyComment.setType(mfflInformationReply.getType());
        mfflInformationReplyComment.setUserId(sysUser.getId().intValue());
        mfflInformationReplyComment.setContent(content);
        mfflInformationReplyComment.setTopid(mfflInformationReply.getId().intValue());
        int status = mfflInformationReplyDao.save(mfflInformationReplyComment);
        if (status == 1) {
            mfflInformationReply.setNumber(mfflInformationReply.getNumber() + 1);
            int statuss = mfflInformationReplyDao.update(mfflInformationReply);
            if (statuss == 1) {
                MfflSystemMessages.sendMessage("您收到一条系统消息，内容为:您发布的资讯[" + mfflInformation.getTitle() + "]有人回答，请查看！", "7", mfflInformation.getId().intValue(), mfflUser.getId().intValue());
                MfflSystemMessages.sendMessage("您收到一条系统消息，内容为:您回答的资讯[" + mfflInformation.getTitle() + "]有人评论，请查看！", "7", mfflInformation.getId().intValue(), mfflUserTwo.getId().intValue());
                return new ReturnBean();
            } else {
                return new ReturnBean("105", "操作失败请稍后再试");
            }
        } else {
            return new ReturnBean("105", "操作失败请稍后再试");
        }
    }


    @GetMapping(value = "/getInformation")
    @ApiOperation(value = "资讯-资讯详情(回答列表)")
    public ReturnBean<InformationAndReplyVo> getInformation(@ApiParam(value = "资讯id", name = "informationId", required = true) @RequestParam long informationId) {
        //第一层数据  资讯详情和发布用户信息
        this.mfflInformationDao.saveInformationHot(informationId);
        MfflInformationVo mfflInformationVo = this.mfflInformationDao.getMfflInformationVoById(informationId);
        if (mfflInformationVo == null) {
            return new ReturnBean("201", "查询失败,资讯不存在");
        }
        mfflInformationVo.setImageUrlAll(imageUrlDispose(mfflInformationVo.getImageUrl()));
        MfflUser mfflInformationUser = this.mfflUserDao.getUserInfoAll(mfflInformationVo.getUserId());
        if (mfflInformationUser == null) {
            return new ReturnBean("201", "用户不存在");
        }
        SysUser sysUser = UserUtil.getLoginUser();
        mfflInformationVo.setHeadImgUrl(mfflInformationUser.getHeadImgUrl());
        mfflInformationVo.setIdentity(mfflInformationUser.getIdentity());
        mfflInformationVo.setNickname(mfflInformationUser.getNickname());
        MfflInfomationContent mfflInfomationContent = mfflInfomationContentDao.getById(informationId);
        if (mfflInfomationContent != null) {
            mfflInformationVo.setContent(mfflInfomationContent.getContent());
        }
        //0是没收藏 1是已收藏
        mfflInformationVo.setCollect(mfflInformationCollectDao.getCountByReplyId(sysUser.getId(), informationId) == null ? 0 : 1);
        //第二层数据  评论详情和评论用户数据
        List<MfflInformationReplyVo> mfflInformationReplyList = this.mfflInformationReplyDao.getMfflInformationReplyListByInformationId(informationId, sysUser.getId());
        for (MfflInformationReplyVo m : mfflInformationReplyList) {
            m.setPraiseType(m.getPraiseType() == null ? "0" : "1");
            m.setTreadType(m.getTreadType() == null ? "0" : "1");
        }
        InformationAndReplyVo informationAndReplyVo = new InformationAndReplyVo();
        informationAndReplyVo.setIssuer(mfflInformationVo);
        informationAndReplyVo.setReply(mfflInformationReplyList);
        return new ReturnBean("200", informationAndReplyVo, "成功");
    }


    @GetMapping(value = "/getReply")
    @ApiOperation(value = "资讯-回答详请(评论列表)")
    public ReturnBean<ReplyAndCommentVo> getTopic(@ApiParam(value = "回答id", name = "replyId", required = true) @RequestParam long replyId) {
        SysUser sysUser = UserUtil.getLoginUser();
        MfflInformationReplyVo mfflInformationReplyVo = this.mfflInformationReplyDao.getMfflInformationReplyVoById(replyId, sysUser.getId());
        if (mfflInformationReplyVo == null) {
            return new ReturnBean("201", "查询失败,回答不存在");
        }
        MfflUser mfflInformationUser = this.mfflUserDao.getUserInfoAll(mfflInformationReplyVo.getUserId());
        if (mfflInformationUser == null) {
            return new ReturnBean("201", "用户不存在");
        }
        mfflInformationReplyVo.setHeadImgUrl(mfflInformationUser.getHeadImgUrl());
        mfflInformationReplyVo.setIdentity(mfflInformationUser.getIdentity());
        mfflInformationReplyVo.setNickname(mfflInformationUser.getNickname());
        mfflInformationReplyVo.setPraiseType(mfflInformationReplyVo.getPraiseType() == null ? "0" : "1");
        mfflInformationReplyVo.setTreadType(mfflInformationReplyVo.getTreadType() == null ? "0" : "1");


        List<MfflInformationReplyVo> mfflInformationReplyVoList = this.mfflInformationReplyDao.getMfflInformationReplyListById(replyId);
        ReplyAndCommentVo replyAndCommentVo = new ReplyAndCommentVo();
        replyAndCommentVo.setReply(mfflInformationReplyVo);
        replyAndCommentVo.setComment(mfflInformationReplyVoList);

        return new ReturnBean("200", replyAndCommentVo, "成功");
    }


    @PostMapping(value = "/addCommend")
    @ApiOperation(value = "资讯-回答的点赞")
    @Transactional
    public ReturnBean commend(@ApiParam(value = "回答id", name = "replyId", required = true) @RequestParam long replyId) {

        SysUser sysUser = UserUtil.getLoginUser();
        MfflInformationReply mfflInformationReply = this.mfflInformationReplyDao.getById(replyId);
        if (mfflInformationReply == null) {
            return new ReturnBean("201", "查询失败");
        }

        MfflInformationCommendandtread mfflInformationCommendandtread = this.mfflInformationCommendandtreadDao.getCountByReplyId(sysUser.getId(), replyId, "0");
        if (mfflInformationCommendandtread == null) {
            mfflInformationCommendandtread = new MfflInformationCommendandtread();
            mfflInformationCommendandtread.setUserId(sysUser.getId().intValue());
            mfflInformationCommendandtread.setReplyId((int) replyId);
            mfflInformationCommendandtread.setType("0");
            int status = mfflInformationCommendandtreadDao.save(mfflInformationCommendandtread);
            if (status == 1) {
                this.mfflInformationReplyDao.updateAddPraise(mfflInformationReply.getPraise() + 1, replyId);
                ReturnBean returnBean = new ReturnBean();
                returnBean.setDatas(1);
                return returnBean;
            } else {
                return new ReturnBean("201", "操作失败");
            }
        } else {
            int status = mfflInformationCommendandtreadDao.delete(mfflInformationCommendandtread.getId());
            if (status == 1) {
                this.mfflInformationReplyDao.updateAddPraise(mfflInformationReply.getPraise() - 1, replyId);
                ReturnBean returnBean = new ReturnBean();
                returnBean.setDatas(0);
                return returnBean;
            } else {
                return new ReturnBean("201", "操作失败");
            }
        }


    }


    @PostMapping(value = "/addTread")
    @ApiOperation(value = "资讯-回答的踩")
    @Transactional
    public ReturnBean addTread(@ApiParam(value = "回答id", name = "replyId", required = true) @RequestParam long replyId) {

        SysUser sysUser = UserUtil.getLoginUser();
        MfflInformationReply mfflInformationReply = this.mfflInformationReplyDao.getById(replyId);
        if (mfflInformationReply == null) {
            return new ReturnBean("201", "查询失败");
        }

        MfflInformationCommendandtread mfflInformationCommendandtread = this.mfflInformationCommendandtreadDao.getCountByReplyId(sysUser.getId(), replyId, "1");
        if (mfflInformationCommendandtread == null) {
            mfflInformationCommendandtread = new MfflInformationCommendandtread();
            mfflInformationCommendandtread.setUserId(sysUser.getId().intValue());
            mfflInformationCommendandtread.setReplyId((int) replyId);
            mfflInformationCommendandtread.setType("1");
            int status = mfflInformationCommendandtreadDao.save(mfflInformationCommendandtread);
            if (status == 1) {
                this.mfflInformationReplyDao.updateAddTrade(mfflInformationReply.getTread() + 1, replyId);
                ReturnBean returnBean = new ReturnBean();
                returnBean.setDatas(1);
                return returnBean;
            } else {
                return new ReturnBean("201", "操作失败");
            }
        } else {
            int status = mfflInformationCommendandtreadDao.delete(mfflInformationCommendandtread.getId());
            if (status == 1) {
                this.mfflInformationReplyDao.updateAddTrade(mfflInformationReply.getTread() - 1, replyId);
                ReturnBean returnBean = new ReturnBean();
                returnBean.setDatas(0);
                return returnBean;
            } else {
                return new ReturnBean("201", "操作失败");
            }
        }


    }


    @PostMapping(value = "/addCollect")
    @ApiOperation(value = "资讯-收藏/取消收藏(我的收藏功能中取消也用这个接口)")
    public ReturnBean addCollect(@ApiParam(value = "资讯id", name = "informationId", required = true) @RequestParam long informationId) {

        SysUser sysUser = UserUtil.getLoginUser();
        MfflInformation mfflInformation = this.mfflInformationDao.getById(informationId);
        if (mfflInformation == null) {
            return new ReturnBean("201", "查询失败");
        }
        MfflInformationCollect mfflInformationCollect = this.mfflInformationCollectDao.getCountByReplyId(sysUser.getId(), informationId);
        if (mfflInformationCollect == null) {
            mfflInformationCollect = new MfflInformationCollect();
            mfflInformationCollect.setUserId(sysUser.getId().intValue());
            mfflInformationCollect.setInformationId((int) informationId);
            int status = mfflInformationCollectDao.save(mfflInformationCollect);
            if (status == 1) {
                ReturnBean returnBean = new ReturnBean();
                returnBean.setDatas(1);
                return returnBean;
            } else {
                return new ReturnBean("201", "收藏失败");
            }
        } else {
            int status = mfflInformationCollectDao.delete(mfflInformationCollect.getId());
            if (status == 1) {
                ReturnBean returnBean = new ReturnBean();
                returnBean.setDatas(0);
                return returnBean;
            } else {
                return new ReturnBean("201", "取消收藏失败");
            }
        }
    }


    @GetMapping(value = "/getMyInformation")
    @ApiOperation(value = "我的话题-我的发布")
    public ReturnBean<MfflInformationVo> getMyInformation() {
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser == null) {
            return new ReturnBean("201", "没有此用户");
        }
        MfflUser user = this.mfflUserDao.getUserInfoAll(sysUser.getId().intValue());
        List<MfflInformationVo> mfflInformationList = this.mfflInformationDao.getMyInformationList(sysUser.getId());
        for (MfflInformationVo m : mfflInformationList) {
            m.setNickname(user.getNickname());
            m.setIdentity(user.getIdentity());
            m.setHeadImgUrl(user.getHeadImgUrl());
            m.setImageUrlAll(imageUrlDispose(m.getImageUrl()));
        }
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(mfflInformationList);
        return returnBean;
    }

    @PutMapping(value = "/updateMyInformation")
    @ApiOperation(value = "我的话题-修改我的发布(只能在没有人没有人回答的情况才能修改)")
    public ReturnBean updateMyInformation(@ApiParam(value = "资讯id", name = "informationId", required = true) @RequestParam long informationId,
                                          @ApiParam(value = "标题", name = "title", required = true) @RequestParam String title,
                                          @ApiParam(value = "内容", name = "content", required = true) @RequestParam String content,
                                          @ApiParam(value = "图片地址多个按分号分隔", name = "imageUrl", required = true) @RequestParam String imageUrl) {
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser == null) {
            return new ReturnBean("201", "没有此用户");
        }
        MfflInformation mfflInformation = this.mfflInformationDao.getById(informationId);
        if (mfflInformation == null) {
            return new ReturnBean("201", "资讯不存在");
        }
        if (sysUser.getId().intValue() != mfflInformation.getUserId()) {
            return new ReturnBean("201", "没有权限修改此话题");
        }
        List<MfflInformationReplyVo> mfflInformationReplyVoList = this.mfflInformationReplyDao.getMfflInformationReplyListByInformationId(informationId, sysUser.getId().intValue());
        if (mfflInformationReplyVoList.size() != 0) {
            return new ReturnBean("201", "已有回答不可修改");
        }

        mfflInformation.setTitle(title);
        mfflInformation.setImageUrl(imageUrl);
        if (StringUtils.isEmpty(content)) {
            return new ReturnBean("201", "内容不许为空");
        }

        String partialContent = MfflUtil.delHTMLTag(content);
        if (partialContent.length() > 250) {
            partialContent = partialContent.substring(0, 249);
        }
        mfflInformation.setPartialContent(partialContent);
        int status = this.mfflInformationDao.update(mfflInformation);
        if (status == 0) {
            return new ReturnBean("201", "修改失败");
        }
        this.mfflInfomationContentDao.updateContentById(informationId, content);
        return new ReturnBean();
    }

    @DeleteMapping("/delMyInformation")
    @ApiOperation(value = "我的话题-删除我的发布(所有用户回答会随之删除)")
    @Transactional
    public ReturnBean delMyInformation(@ApiParam(value = "资讯id", name = "informationId", required = true) @RequestParam long informationId) {
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser == null) {
            return new ReturnBean("201", "没有此用户");
        }
        MfflInformation mfflInformation = this.mfflInformationDao.getById(informationId);
        if (mfflInformation == null) {
            return new ReturnBean("201", "资讯不存在");
        }
        if (sysUser.getId().intValue() != mfflInformation.getUserId()) {
            return new ReturnBean("201", "没有权限修改此话题");
        }

        int status = this.mfflInformationDao.delMyInformation(informationId);
        if (status == 0) {
            return new ReturnBean("201", "删除失败");
        }

        this.mfflInformationReplyDao.delReplyByInformation(informationId);
        return new ReturnBean();
    }


    @GetMapping(value = "/getMyReply")
    @ApiOperation(value = "我的话题-我的回答")
    public ReturnBean<MfflInformationReplyVo> getMyReply() {
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser == null) {
            return new ReturnBean("201", "没有此用户");
        }
        MfflUser user = this.mfflUserDao.getUserInfoAll(sysUser.getId().intValue());
        List<MfflInformationReplyVo> mfflInformationReplyVoList = this.mfflInformationReplyDao.getReplyListByUserId(sysUser.getId());
        for (MfflInformationReplyVo m : mfflInformationReplyVoList) {
            m.setNickname(user.getNickname());
            m.setIdentity(user.getIdentity());
            m.setHeadImgUrl(user.getHeadImgUrl());
            m.setPraiseType(m.getPraiseType() == null ? "0" : "1");
            m.setTreadType(m.getTreadType() == null ? "0" : "1");
        }
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(mfflInformationReplyVoList);
        return returnBean;
    }


    @DeleteMapping("/delMyReply")
    @ApiOperation(value = "我的话题-删除我的回答和评论(会同时删除评论)")
    @Transactional
    public ReturnBean delMyReply(@ApiParam(value = "回答或评论id", name = "replyId", required = true) @RequestParam long replyId) {
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser == null) {
            return new ReturnBean("201", "没有此用户");
        }
        MfflInformationReplyVo mfflInformationReplyVo = this.mfflInformationReplyDao.getMfflInformationReplyVoById(replyId, sysUser.getId());
        if (mfflInformationReplyVo == null) {
            return new ReturnBean("201", "回答不存在");
        }
        if (sysUser.getId().intValue() != mfflInformationReplyVo.getUserId()) {
            return new ReturnBean("201", "没有权限修改此话题");
        }
        //回答
        if (mfflInformationReplyVo.getTopid() == 0) {
            int status = this.mfflInformationReplyDao.delMyReply(replyId);
            if (status == 0) {
                return new ReturnBean("201", "删除失败");
            }
            this.mfflInformationReplyDao.delMyReplyOfReply(replyId);
            return new ReturnBean();
            //评论
        } else {
            int status = this.mfflInformationReplyDao.delMyReply(replyId);

            if (status == 0) {
                return new ReturnBean("201", "删除失败");
            }
            //把回答数量 - 1
            MfflInformationReply mfflInformationReply = this.mfflInformationReplyDao.getById(replyId);
            if (mfflInformationReply != null) {
                this.mfflInformationReplyDao.updateNumber(mfflInformationReply.getTopid());
            }
            return new ReturnBean();
        }
    }


    @GetMapping(value = "/getComment")
    @ApiOperation(value = "我的话题-我的评论")
    public ReturnBean<MfflInformationReplyVo> getComment() {
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser == null) {
            return new ReturnBean("201", "没有此用户");
        }
        MfflUser user = this.mfflUserDao.getUserInfoAll(sysUser.getId().intValue());
        List<MfflInformationReplyVo> mfflInformationReplyVoList = this.mfflInformationReplyDao.getCommentListByUserId(sysUser.getId());
        for (MfflInformationReplyVo m : mfflInformationReplyVoList) {
            m.setNickname(user.getNickname());
            m.setIdentity(user.getIdentity());
            m.setHeadImgUrl(user.getHeadImgUrl());
        }
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(mfflInformationReplyVoList);
        return returnBean;
    }


    @GetMapping(value = "/getMyCollect")
    @ApiOperation(value = "我的-我的收藏")
    public ReturnBean<MfflInformationVo> getMyCollect() {
        SysUser sysUser = UserUtil.getLoginUser();
        if (sysUser == null) {
            return new ReturnBean("201", "没有此用户");
        }
        List<MfflInformationVo> mfflInformationVoList = this.mfflInformationDao.getMyCollectList(sysUser.getId());
        for (MfflInformationVo m : mfflInformationVoList) {
            m.setImageUrlAll(imageUrlDispose(m.getImageUrl()));
        }
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(mfflInformationVoList);
        return returnBean;
    }


    private Map<String, Object> imageUrlDispose(String imageUrl) {
        Map<String, Object> map = new HashMap<>();
        String[] imageUrlGroup = imageUrl.split(";");
        if (!StringUtils.isEmpty(imageUrl)) {
            String imageUrlMed = "";
            String imageUrlMin = "";
            for (int i = 0; i < imageUrlGroup.length; i++) {
                String[] imageUrls = imageUrlGroup[i].split("\\.");
                imageUrlMed += imageUrls[0] + "_med." + imageUrls[1] + ";";
                imageUrlMin += imageUrls[0] + "_min." + imageUrls[1] + ";";
            }
            map.put("imageUrl", imageUrl);
            map.put("imageUrlMed", imageUrlMed.substring(0, imageUrlMed.length() - 1));
            map.put("imageUrlMin", imageUrlMin.substring(0, imageUrlMed.length() - 1));
        }
        return map;
    }
}
